package com.canbot.u05.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.canbot.u05.R;
import com.canbot.u05.adapter.ActionAdapter;
import com.canbot.u05.bean.Brow;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 动作的Fragment
 */
public class ActionFragment extends Fragment implements AdapterView.OnItemClickListener {

        public List<Brow> mbrowList;

        private ViewPager mViewPager;

        private int homePage;

        private ToastTools toastTools;

        private HashMap<String, String> hashMap = new HashMap<>();

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment_action, container, false);
                mViewPager = (ViewPager) view.findViewById(R.id.vp_content_action);
                getDate();
                mViewPager.setAdapter(pagerAdapter);
                pagerAdapter.notifyDataSetChanged();
                mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {
                        }

                        @Override
                        public void onPageSelected(int i) {
                                homePage = i;
                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {

                        }
                });
                Logger.e("test", "onCreateView");
                mViewPager.setCurrentItem(0);
                return view;
        }

        @Override
        public void onResume() {
                super.onResume();
        }


        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                handOnItemClick(i);
        }

        private PagerAdapter pagerAdapter = new PagerAdapter() {

                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
                        return arg0 == arg1;
                }

                @Override
                public int getCount() {
                        return mbrowList.size() / 8 == 0 ? mbrowList.size() / 8 - 1 : getDate().size() / 8;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView((View) object);
                }

                @Override
                public Object instantiateItem(ViewGroup container, final int position) {

                        LayoutInflater inflater = LayoutInflater.from(container.getContext());
                        View view = inflater.inflate(R.layout.fragment_action_adapter, container, false);
                        GridView mGridView = (GridView) view.findViewById(R.id.gv_content_action);
                        List<Brow> mbrowList1 = new ArrayList<>();
                        for (int i = 0; i < getCount(); i++) {
                                if (i == position) {
                                        mbrowList1 = mbrowList.subList(i * 8, i * 8 +8< mbrowList.size() ? i * 8 + 8 : mbrowList.size());
                                }
                        }
                        ActionAdapter actionAdapter = new ActionAdapter(getActivity(), mbrowList1);
                        mGridView.setAdapter(actionAdapter);
                        mGridView.setOnItemClickListener(ActionFragment.this);
                        container.addView(view);
                        actionAdapter.notifyDataSetChanged();
                        return view;
                }

                @Override
                public int getItemPosition(Object object) {
                        return POSITION_NONE;
                }
        };

        /**
         * 处理点击时间
         *
         * @param position
         */
        private void handOnItemClick(int position) {
                Brow brow = mbrowList.get(homePage * 8 + position);
                ToastTools.newInstance(getActivity()).showText(brow.getName());
                toastTools.showText(brow.getName());

                //直接发送給头
                boolean playSound = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.BASE_AUDIO_PATH + brow.getPath());
                boolean eyeMotion = MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION_TWO, brow.getEyesID());
                boolean action = MsgSendUtils.sendStringMsg(MsgType.ACTION, brow.getActionId());
                MsgSendUtils.sendStringMsgToPad(MsgType.RECEIVER_MSG_SWITCH_ACTIVITY, brow.getPadId()+"&&conn");
                if (playSound && eyeMotion && action) {
                        toastTools.showText(getString(R.string.send_successfully));
                }
                else {
                        toastTools.showText(getString(R.string.network_error));
                }
//        //通过胸口发送給头，不需要三方联网
//        hashMap.clear();
//        hashMap.put("action",brow.getActionId());
//        hashMap.put("music",brow.getPath());
//        hashMap.put("eyes",brow.getEyesID());
//        //OkHttpUtils.upLoadFile(Url, hashMap,callBack);
//        MsgSendUtils.sendStringMsgToPad(MsgType.SEND_PAD_EXECUTE_ACTION,JSON.toJSONString(brow));


        }

        OkHttpUtils.OkHttpCallBack callBack = new OkHttpUtils.OkHttpCallBack() {
                @Override
                public void onSuccess(String result) {
                        toastTools.showText(result);
                }

                @Override
                public void onFailure() {
                        toastTools.showText(getString(R.string.network_error));
                }
        };

        private String Url = "http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "actionmusic";

        @Override
        public void onAttach(Activity activity) {
                super.onAttach(activity);
                toastTools = ToastTools.newInstance(getActivity());
        }

        private List<Brow> getDate() {
                mbrowList = new ArrayList<Brow>();
                Brow brow1 = new Brow(1, "嘘", "10506", "action/u05_xx_001.wav","18","30");
                Brow brow2 = new Brow(2, "敬礼", "10205", "action/dzzl_u05_008.wav", "14","27");
                Brow brow3 = new Brow(3, "点头", "10203", "action/u05_dt_001.wav","30","33");
                Brow brow4 = new Brow(4, "安静", "10104", "action/u05_aj_001.wav", "17","33");
                Brow brow5 = new Brow(5, "摇头", "10204", "action/dzzl_u05_010.wav", "18","33");
                Brow brow6 = new Brow(6, "欢迎", "10105","action/u05_hy_001.wav","12","27");
                Brow brow7 = new Brow(7, "握手", "10228", "action/ws_u05_001.wav", "14","27");
                Brow brow8 = new Brow(8, "转圈", "10211", "action/dzzl_u05_009.wav", "3","27");
                Brow brow9 = new Brow(9, "退下", "10207", "action/dzzl_u05_017.wav", "18","29");
                Brow brow10 = new Brow(10, "耍帅", "10106", "action/u05_ss_001.wav", "43","27");
                Brow brow11 = new Brow(11, "飞吻", "11701", "action/u05_fw_001.wav", "34","27");
                Brow brow12 = new Brow(12, "挥手", "10302", "action/u05_hs_001.wav", "14","27");
                Brow brow13 = new Brow(13, "再见", "10306", "action/u05_zj_001.wav", "26","29");
                Brow brow14 = new Brow(14, "剪刀手", "10507", "action/u05_jds_001.wav", "12","27");
                Brow brow15 = new Brow(15, "秀肌肉", "10113", "action/u05_xjr_001.wav", "2","27");
                Brow brow16 = new Brow(16, "再摇头", "10203", "action/dzzl_u05_011.wav", "18","33");
                Brow brow17 = new Brow(17, "求抱抱", "10114", "action/u05_qbb_001.wav", "12","27");
                Brow brow18 = new Brow(18, "向后退", "10207", "action/dzzl_u05_002.wav", "3","27");
                Brow brow19 = new Brow(19, "向前进", "10206", "action/dzzl_u05_001.wav", "18","27");
                Brow brow20 = new Brow(20, "向右看", "10201", "action/dzzl_u05_004.wav", "18","27");
                Brow brow21 = new Brow(21, "向左看", "10202", "action/dzzl_u05_003.wav", "18","27");
                Brow brow22 = new Brow(22, "向左转", "10208", "action/dzzl_u05_005.wav", "3","27");
                Brow brow23 = new Brow(23, "向右转", "10209", "action/dzzl_u05_006.wav", "3","27");
                Brow brow24 = new Brow(24, "转过身来", "10210", "action/dzzl_u05_012.wav", "14","30");
                Brow brow25 = new Brow(25, "仰头大笑", "14010","action/u05_ytdx_001.wav", "30","27");
                Brow brow26 = new Brow(26, "环顾四周", "14006", "action/u05_hgsz_001.wav", "17","27");
                mbrowList.add(brow1);
                mbrowList.add(brow2);
                mbrowList.add(brow3);
                mbrowList.add(brow4);
                mbrowList.add(brow5);
                mbrowList.add(brow6);
                mbrowList.add(brow7);
                mbrowList.add(brow8);
                mbrowList.add(brow9);
                mbrowList.add(brow10);
                mbrowList.add(brow11);
                mbrowList.add(brow12);
                mbrowList.add(brow13);
                mbrowList.add(brow14);
                mbrowList.add(brow15);
                mbrowList.add(brow16);
                mbrowList.add(brow17);
                mbrowList.add(brow18);
                mbrowList.add(brow19);
                mbrowList.add(brow20);
                mbrowList.add(brow21);
                mbrowList.add(brow22);
                mbrowList.add(brow23);
                mbrowList.add(brow24);
                mbrowList.add(brow25);
                mbrowList.add(brow26);

                return mbrowList;
        }
}
